//
//  ViewController.m
//  Account
//
//  Created by wanmeizty on 2016/11/29.
//  Copyright © 2016年 wanmeizty. All rights reserved.
//

#import "ViewController.h"

#import "DescViewController.h"

#import "AddViewController.h"

#import "ResultViewController.h"

#import "FMDBManager.h"

#import "XMTitleAndSubTitleView.h"

#import "MyButton.h"

@interface ViewController (){
    
    double _totalFee;
    
}

@property (nonatomic, strong) XMTitleAndSubTitleView * name1;
@property (nonatomic, strong) XMTitleAndSubTitleView * name2;
@property (nonatomic, strong) XMTitleAndSubTitleView * name3;
@property (nonatomic, strong) XMTitleAndSubTitleView * name4;
@property (nonatomic, strong) XMTitleAndSubTitleView * name5;
@property (nonatomic, strong) UILabel * descLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"宿舍记账";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查看账单" style:UIBarButtonItemStylePlain target:self action:@selector(searchNodes)];//[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"增加记录" style:UIBarButtonItemStylePlain target:self action:@selector(addNodes)];//[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
    
    FMDBManager * fmdbManager = [FMDBManager shareManager];
    
    [fmdbManager createFMDBDataBaseWithDbName:@"accountDB"];
    [fmdbManager createTableWithTableName:@"accountTable"];
    
    [self createViews];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)createViews{
    
    CGFloat top = 10 * [XMScreenAdaptation adapterMultipleByWidth];
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width, 40 * [XMScreenAdaptation adapterMultipleByWidth])];
    titleLabel.text = @"当前各人累计购买总金额";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.rootScrollView addSubview:titleLabel];
    
    top += 40 * [XMScreenAdaptation adapterMultipleByWidth] + 10 * [XMScreenAdaptation adapterMultipleByWidth];
    
    XMTitleAndSubTitleView * name1 = [[XMTitleAndSubTitleView alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width , 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.rootScrollView addSubview:name1];
    self.name1 = name1;
    
    double fee1 = [[FMDBManager shareManager] getFeeFromTableWithName:@"张庭勇"];
    
    [name1 setupDatasWithTitle:@"张庭勇" value:[NSString stringWithFormat:@"%.2f",fee1]];
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth];
    
    XMTitleAndSubTitleView * name2 = [[XMTitleAndSubTitleView alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width , 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.rootScrollView addSubview:name2];
    self.name2 = name2;
    
    double fee2 = [[FMDBManager shareManager] getFeeFromTableWithName:@"陈伟豪"];
    
    [name2 setupDatasWithTitle:@"陈伟豪" value:[NSString stringWithFormat:@"%.2f",fee2]];
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth];
    
    XMTitleAndSubTitleView * name3 = [[XMTitleAndSubTitleView alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width , 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.rootScrollView addSubview:name3];
    self.name3 = name3;
    double fee3 = [[FMDBManager shareManager] getFeeFromTableWithName:@"王勇"];
    
    [name3 setupDatasWithTitle:@"王勇" value:[NSString stringWithFormat:@"%.2f",fee3]];
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth];
    
    XMTitleAndSubTitleView * name4 = [[XMTitleAndSubTitleView alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width , 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.rootScrollView addSubview:name4];
    self.name4 = name4;
    double fee4 = [[FMDBManager shareManager] getFeeFromTableWithName:@"张帆"];
    
    [name4 setupDatasWithTitle:@"张帆" value:[NSString stringWithFormat:@"%.2f",fee4]];
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth];
    
    XMTitleAndSubTitleView * name5 = [[XMTitleAndSubTitleView alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width , 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.rootScrollView addSubview:name5];
    self.name5 = name5;
    double fee5 = [[FMDBManager shareManager] getFeeFromTableWithName:@"周建"];
    
    [name5 setupDatasWithTitle:@"周建" value:[NSString stringWithFormat:@"%.2f",fee5]];
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth] + 30 * [XMScreenAdaptation adapterMultipleByWidth];
    
    MyButton * accountBtn = [[MyButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.5 - 40, top, 80, 30 * [XMScreenAdaptation adapterMultipleByWidth])];
    [accountBtn setTitle:@"计算" forState:UIControlStateNormal];
    [accountBtn addTarget:self action:@selector(account) forControlEvents:UIControlEventTouchUpInside];
    [self.rootScrollView addSubview:accountBtn];
    
    top += 30 * [XMScreenAdaptation adapterMultipleByWidth] + 10 * [XMScreenAdaptation adapterMultipleByWidth];
    
    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, top, self.view.frame.size.width, 120 * [XMScreenAdaptation adapterMultipleByWidth])];
    self.descLabel.hidden = YES;
    self.descLabel.numberOfLines = 0;
    self.descLabel.textAlignment = NSTextAlignmentCenter;
    self.descLabel.textColor = [UIColor redColor];
    [self.rootScrollView addSubview:self.descLabel];
    
}

- (void)account{
    

    
//    NSDictionary * dict = [[FMDBManager shareManager] getEveryOneFeestringFromTable];//[NSString stringWithFormat:@"平均每人：%.2f元",_totalFee / 5];
//    self.descLabel.text = [NSString stringWithFormat:@"平均每人：%.2f元",_totalFee / 5];
//    NSLog(@"dict==>%@",dict);
//    self.descLabel.hidden = NO;
    ResultViewController * resultVC = [[ResultViewController alloc] init];
    
    [self.navigationController pushViewController:resultVC animated:YES];
    

    
    
}

//- (void)macf{
//    
//    double max = 0;
//    
//    int index = 0;
//    
//    if (max < ztytotalfee) {
//        
//        max = ztytotalfee;
//        index = 0;
//        
//    }
//    
//    if (max < zjtotalfee) {
//        
//        max = zjtotalfee;
//        index = 1;
//    }
//    
//    if (max < cwhtotalfee) {
//        
//        max = cwhtotalfee;
//        index = 2;
//    }
//    
//    if (max < wytotalfee) {
//        
//        max = wytotalfee;
//        index = 3;
//    }
//    
//    if (max < zftotalfee) {
//        
//        max = zftotalfee;
//        index = 4;
//    }
//    
//    NSArray * originArray = @[@"张庭勇", @"张帆", @"陈伟豪", @"王勇", @"周建"];
//}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self updatas];
    self.descLabel.hidden = YES;
    
}

- (void)updatas{
    
    double fee1 = [[FMDBManager shareManager] getFeeFromTableWithName:@"张庭勇"];
    
    [self.name1 setupDatasWithTitle:@"张庭勇" value:[NSString stringWithFormat:@"%.2f元",fee1]];
    

    double fee2 = [[FMDBManager shareManager] getFeeFromTableWithName:@"陈伟豪"];
    
    [self.name2 setupDatasWithTitle:@"陈伟豪" value:[NSString stringWithFormat:@"%.2f元",fee2]];
    

    double fee3 = [[FMDBManager shareManager] getFeeFromTableWithName:@"王勇"];
    
    [self.name3 setupDatasWithTitle:@"王勇" value:[NSString stringWithFormat:@"%.2f元",fee3]];

    double fee4 = [[FMDBManager shareManager] getFeeFromTableWithName:@"张帆"];
    
    [self.name4 setupDatasWithTitle:@"张帆" value:[NSString stringWithFormat:@"%.2f元",fee4]];

    

    double fee5 = [[FMDBManager shareManager] getFeeFromTableWithName:@"周建"];
    
    [self.name5 setupDatasWithTitle:@"周建" value:[NSString stringWithFormat:@"%.2f元",fee5]];
    
    _totalFee = fee1 + fee2 + fee3 + fee4 + fee5;
    
}

- (void)searchNodes{
    
    DescViewController * descvc = [[DescViewController alloc] init];
    
    [self.navigationController pushViewController:descvc animated:YES];
}

- (void)addNodes{
    
    AddViewController * addvc = [[AddViewController alloc] init];
    
    [self.navigationController pushViewController:addvc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
